<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="患者" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="patientId">
              <a-select v-model="model.patientId" placeholder="请选择患者">
                <a-select-option :value="i.id" v-for="(i, o) in patList" :key="o">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="医生" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="doctorId">
              <a-select v-model="model.doctorId" placeholder="请选择医生">
                <a-select-option :value="i.id" v-for="(i, o) in docList" :key="o">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="专业技能分" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="skill">
              <a-rate v-model="model.skill" :tooltips="desc" :allow-clear="false" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="服务态度分" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attitude">
              <a-rate v-model="model.attitude" :tooltips="desc" :allow-clear="false" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="沟通质量分" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quality">
              <a-rate v-model="model.quality" :tooltips="desc" :allow-clear="false" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="建议" required :labelCol="labelCol" :wrapperCol="wrapperCol" prop="suggest">
              <a-textarea v-model="model.suggest" rows="4" placeholder="请输入建议" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'WzQualitySurveyForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      desc: ['1分', '2分', '3分', '4分', '5分'],
      docList: [],
      patList: [],
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        patientId: [{ required: true, message: '请选择患者!' }],
        doctorId: [{ required: true, message: '请选择医生!' }],
        skill: [{ required: true, message: '请选择专业技能分!' }],
        attitude: [{ required: true, message: '请选择服务态度分!' }],
        quality: [{ required: true, message: '请选择沟通质量分!' }],
        suggest: [{ required: true, message: '请输入建议!' }],
      },
      url: {
        add: "/wz/quality/wzQualitySurvey/add",
        edit: "/wz/quality/wzQualitySurvey/edit",
        queryById: "/wz/quality/wzQualitySurvey/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
    getAction('/wz/doctor/wzDoctorInfo/list', { pageNo: 1, pageSize: 999 }).then(res => {
      this.docList = res.result.records;
    })
    getAction('/wz/patient/wzPatientInfo/list', { pageNo: 1, pageSize: 999 }).then(res => {
      this.patList = res.result.records;
    })
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>